command line with -e

Discussion in 'Perl Misc' started by April, Oct 11, 2008.

  1. April

    April Guest

    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\""
     
    April, Oct 11, 2008
    #1
    1. Advertising

  2. April

    Ben Morrow Guest

    Quoth 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\""


    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".
    []
     
    Ben Morrow, Oct 11, 2008
    #2
    1. Advertising

  3. April

    Dr.Ruud Guest

    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."
     
    Dr.Ruud, Oct 11, 2008
    #3
  4. April

    April Guest

    On Oct 11, 12:57 am, Ben Morrow <> wrote:
    > Quoth 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\""

    >
    > 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".
    >                                                          []


    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?
     
    April, Oct 11, 2008
    #4
  5. April

    Ben Morrow Guest

    [please trim quotations when you reply]

    Quoth April <>:
    > On Oct 11, 12:57 am, Ben Morrow <> 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".
    []
     
    Ben Morrow, Oct 11, 2008
    #5
  6. April <> wrote:
    >> Quoth April <>:
    >> > 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
     
    Jürgen Exner, Oct 11, 2008
    #6
  7. April

    Tim Greer Guest

    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!
     
    Tim Greer, Oct 14, 2008
    #7
  8. April

    April Guest

    On Oct 14, 3:27 pm, Tim Greer <> 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.
     
    April, Oct 31, 2008
    #8
  9. April

    Tim Greer Guest

    April wrote:

    > On Oct 14, 3:27 pm, Tim Greer <> 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!
     
    Tim Greer, Oct 31, 2008
    #9
  10. On Thu, 30 Oct 2008 17:42:22 -0700 (PDT), April
    <> 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,
     
    Michele Dondi, Oct 31, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Cowald
    Replies:
    4
    Views:
    1,088
    Tohid
    Oct 23, 2003
  2. Bernd Oninger
    Replies:
    1
    Views:
    805
    Martin Honnen
    Jul 7, 2004
  3. Replies:
    4
    Views:
    1,724
    Terry Hancock
    Apr 23, 2005
  4. Lucas Cowald

    RUN/execute a Command-Line command from an ASP page

    Lucas Cowald, Oct 22, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    255
    Genich
    Oct 22, 2003
  5. Lucas Cowald

    RUN/execute a Command-Line command from an ASP page.

    Lucas Cowald, Oct 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    219
    Oliver
    Oct 22, 2003
Loading...

Share This Page