Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > command line with -e

Reply
Thread Tools

command line with -e

 
 
April
Guest
Posts: n/a
 
      10-11-2008

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\""
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      10-11-2008

Quoth April <(E-Mail Removed)>:
>
> 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

--
I have two words that are going to make all your troubles go away.
"Miniature". "Golf".
[(E-Mail Removed)]
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      10-11-2008
April schreef:

> perl -e "$_=2"


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

Alternative:

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



--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
April
Guest
Posts: n/a
 
      10-11-2008
On Oct 11, 12:57*am, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth April <(E-Mail Removed)>:
>
>
>
> > 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
>
> --
> I have two words that are going to make all your troubles go away.
> "Miniature". "Golf".
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * *[(E-Mail Removed)]


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?
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-11-2008
[please trim quotations when you reply]

Quoth April <(E-Mail Removed)>:
> On Oct 11, 12:57*am, Ben Morrow <(E-Mail Removed)> wrote:
> >
> > 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.

>
> 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

--
I have two words that are going to make all your troubles go away.
"Miniature". "Golf".
[(E-Mail Removed)]
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-11-2008
April <(E-Mail Removed)> wrote:
>> Quoth April <(E-Mail Removed)>:
>> > tried the following but seems not working:

>>
>> > perl -e "$_=2"
>> > perl -e "print \"$_2\n\""

>
>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
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-14-2008
April wrote:

>
> 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?
--
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!
 
Reply With Quote
 
April
Guest
Posts: n/a
 
      10-31-2008
On Oct 14, 3:27*pm, Tim Greer <(E-Mail Removed)> wrote:
> Aprilwrote:
>
> > 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?
> --
> 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.



 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-31-2008
April wrote:

> On Oct 14, 3:27Â*pm, Tim Greer <(E-Mail Removed)> wrote:
>> Aprilwrote:
>>
>> > 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?
>> --

<please don't quote signatures>
>
> 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.
--
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!
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      10-31-2008
On Thu, 30 Oct 2008 17:42:22 -0700 (PDT), April
<(E-Mail Removed)> wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Command line option syntax error. Type Command /? for Help chuckcar Computer Support 11 04-21-2009 09:59 PM
Re: Command line option syntax error. Type Command /? for Help Evan Platt Computer Support 1 04-18-2009 10:17 PM
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM
Need simple command for validating xml files at the command line Bernd Oninger XML 1 07-07-2004 06:20 PM
RUN/execute a Command-Line command from an ASP page. Lucas Cowald ASP .Net 4 10-23-2003 11:09 AM



Advertisments