command line with -e

A

April

I managed to make the following work on my pc:

perl -e "print \"Hello world\n\""

should I be able to also assign value to $_ and then verify it,
possibly in this way just for quick checking?

tried the following but seems not working:

perl -e "$_=2"

perl -e "print \"$_2\n\""
 
B

Ben Morrow

Quoth April said:
I managed to make the following work on my pc:

perl -e "print \"Hello world\n\""

should I be able to also assign value to $_ and then verify it,
possibly in this way just for quick checking?

tried the following but seems not working:

perl -e "$_=2"

perl -e "print \"$_2\n\""

Um... you seem to be somewhat confused. Each separate invocation of perl
is completely separate: variables and such never carry over from one to
the next. If you want to execute several statements one after the other,
you can separate them with ; like this:

perl -e "$_ = 2; print \"$_\n\""

but it's usually better to put them in a file and run that.

If you're running perl from the command line, it's worth getting used to
the q// and qq// forms of quoting. You can rewrite you're first example
as

perl -e "print qq/Hello world\n/"

where the qq means 'double quotes' and the slashes go either end of the
quoted material. This avoids needing to put backslashes all over the
place. Read perldoc perlop for the full details.

If you're just experimenting you can run

perl -de1

which will give you a prompt something like

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1): 1
DB<1>

where you can type Perl statements which will be immediately run: since
this is all within one execution of perl, variables will carry across
from statement to statement as you seem to expect.

Ben
 
D

Dr.Ruud

April schreef:
perl -e "$_=2"

Assigning directly to (the gobal) $_ is not a good style.

Alternative:

my $var = "important data";
for ( $var ) {
s/.*/2/;
}

;)
 
A

April

Quoth April <[email protected]>:









Um... you seem to be somewhat confused. Each separate invocation of perl
is completely separate: variables and such never carry over from one to
the next. If you want to execute several statements one after the other,
you can separate them with ; like this:

    perl -e "$_ = 2; print \"$_\n\""

but it's usually better to put them in a file and run that.

If you're running perl from the command line, it's worth getting used to
the q// and qq// forms of quoting. You can rewrite you're first example
as

    perl -e "print qq/Hello world\n/"

where the qq means 'double quotes' and the slashes go either end of the
quoted material. This avoids needing to put backslashes all over the
place. Read perldoc perlop for the full details.

If you're just experimenting you can run

    perl -de1

which will give you a prompt something like

    Loading DB routines from perl5db.pl version 1.28
    Editor support available.

    Enter h or `h h' for help, or `man perldebug' for more help.

    main::(-e:1):   1
      DB<1>

where you can type Perl statements which will be immediately run: since
this is all within one execution of perl, variables will carry across
from statement to statement as you seem to expect.

Ben

Great Ben, thanks for the very educational reply!

Yes I'm kind of experiemnting, it seems to me at one point I was able
to check what is the current value of $_ and also alter it, using the
command line option?
 
B

Ben Morrow

[please trim quotations when you reply]

Quoth April said:
Great Ben, thanks for the very educational reply!

Yes I'm kind of experiemnting, it seems to me at one point I was able
to check what is the current value of $_ and also alter it, using the
command line option?

I don't think so. You seem to still be misunderstanding: when perl isn't
running, there is no 'current value of $_', and each running copy of
perl has its own $_.

Ben
 
J

Jürgen Exner

April said:
Yes I'm kind of experiemnting, it seems to me at one point I was able
to check what is the current value of $_ and also alter it, using the
command line option?

Of course you can. The -e option does nothing but allowing you to
specify a Perl script directly on the command line instead of saving it
in a file. It is exactly the same Perl code and has exactly the same
capabilities as you have in a script in a file (well, modulo the awkward
shell escapes, of course).

So yes, of course you can "check" the current value of $_ at any point
of your program by printing it or comparing it to another value.

And you can also alter it at any point in your command line script.

The reason why are not seeing any output from
perl -e "print \"$_2\n\""
could be twofold. First I am not sure how all those shell escapes work
in your shell. I'm guessing there is the first problem, otherwise you
should have seen at least a 2 and a newline.
And second, assuming the resulting actually Perl code after the shell
interpretation is
print "$_\n"
then this is the one and only statement in your Perl script. Neither did
you assign any value to $_ nor were there any other preceeding Perl
commands that would have assigned a value to it automatically. So $_ is
undefined and therefore won't print any visible text.

jue
 
T

Tim Greer

April said:
I managed to make the following work on my pc:

perl -e "print \"Hello world\n\""

should I be able to also assign value to $_ and then verify it,
possibly in this way just for quick checking?

tried the following but seems not working:

perl -e "$_=2"

perl -e "print \"$_2\n\""

Each instance of the separate perl commands are individual from each
other.

perl -e 'my $a = "b"' is going to be completely different from perl -e
'my $a = "c"'

Were you looking to declare an ENV variable on the command line and then
have it be used in the perl command, perhaps? I.e., COMPILER=gcc and
then use that in the script or something?
 
A

April

Aprilwrote:







Each instance of the separate perl commands are individual from each
other.

perl -e 'my $a = "b"' is going to be completely different from perl -e
'my $a = "c"'

Were you looking to declare an ENV variable on the command line and then
have it be used in the perl command, perhaps?  I.e., COMPILER=gcc and
then use that in the script or something?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

no one reason I'm trying to use this capability is for debugging, as
well as to have a learning tool that can validate some of my ideas.

I believe Jue is correct on this .. and at one time I was able to do
it but I couldn't remember how I did it - I was able to print out the
current value in $_, and then assign a new value to it and validate
later ... as this seems platform specific, I'm interested in doing it
on XP, as well as Solaris.
 
T

Tim Greer

no one reason I'm trying to use this capability is for debugging, as
well as to have a learning tool that can validate some of my ideas.

I believe Jue is correct on this .. and at one time I was able to do
it but I couldn't remember how I did it - I was able to print out the
current value in $_, and then assign a new value to it and validate
later ... as this seems platform specific, I'm interested in doing it
on XP, as well as Solaris.

I don't know what thread this is a reply to, but each time you manually
run the perl command, it's a separate instance. So, unless it's en
environment variable you can each throughout, or unless you run perl
commands within another script or process that retains that variable,
it's going to be reset... again, unless you're doing something more
than that. A variable will change on each separate instance or the
program/command running otherwise.
 
M

Michele Dondi

I believe Jue is correct on this .. and at one time I was able to do
it but I couldn't remember how I did it - I was able to print out the
current value in $_, and then assign a new value to it and validate
later ... as this seems platform specific, I'm interested in doing it
on XP, as well as Solaris.

No, what you say is plainly not possible: you *believe* you did it. It
happens, but you simply don't remember well and you are confused.
Trust those who know better and don't be bothered by this.

As an *aside* that AFAICS no one has pointed out yet, when using perl
on the cli with -e under Windows as you're doing, it is often
convenient to use alternate delimiters for double quoted strings
instead of quoting double quotes. E.g.:

C:\temp>perl -E "$x=2; say \"\$x=2\""
$x=2

C:\temp>perl -E "$x=2; say qq|\$x=2|"
$x=2


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top