Variable interpolation on STDIN ?

Discussion in 'Perl Misc' started by Abhinav, Aug 3, 2004.

  1. Abhinav

    Abhinav Guest

    Hi,

    I am reading the name of a file from STDIN.

    I am using the following :

    my $fileToCheck = <STDIN>;
    chomp ($fileToCheck);
    if (-e $fileToCheck)
    {
    print "Not Found\n";
    }

    The problem is that I want to allow the user to specify the file with
    environment variables in some way. For example, to check if x.pl is present
    under the home directory, the user should be able to give

    $HOME/x.pl

    However, it seems that we cannot interpolate like this while reading from
    STDIN (or from a file)? Am I missing something ?

    I tried $ENV{HOME}/x.pl also, but it also gave the same error :

    No such file or directory

    TIA

    --
    Abhinav
     
    Abhinav, Aug 3, 2004
    #1
    1. Advertising

  2. Abhinav

    Abhinav Guest

    Julius Plenz wrote:

    > * Abhinav <> [2004-08-03]:
    >
    >>my $fileToCheck = <STDIN>;
    >>chomp ($fileToCheck);
    >>[...]

    >
    >
    > You can write that way shorter:
    >
    > chomp (my $fileToCheck = <STDIN>);
    >
    >
    >> [...]
    >>However, it seems that we cannot interpolate like this while reading
    >>from STDIN (or from a file)? Am I missing something ?
    >>
    >>I tried $ENV{HOME}/x.pl also, but it also gave the same error :

    >
    >
    > Did you also try "$ENV{HOME}/x.pl"?
    >


    Yes : Getting the error,
    No such file or directory

    Of course, if I put

    $fileToCheck="$ENV{HOME}/x.pl";
    instead of taking it from STDIN, it works ...

    --

    Abhinav
     
    Abhinav, Aug 3, 2004
    #2
    1. Advertising

  3. Abhinav

    Toni Erdmann Guest

    Abhinav wrote:
    > Julius Plenz wrote:
    >
    >> * Abhinav <> [2004-08-03]:
    >>
    >>> my $fileToCheck = <STDIN>;
    >>> chomp ($fileToCheck);
    >>> [...]

    >>
    >>
    >>
    >> You can write that way shorter:
    >>
    >> chomp (my $fileToCheck = <STDIN>);
    >>
    >>
    >>> [...]
    >>> However, it seems that we cannot interpolate like this while reading
    >>> from STDIN (or from a file)? Am I missing something ?
    >>>
    >>> I tried $ENV{HOME}/x.pl also, but it also gave the same error :

    >>
    >>
    >>
    >> Did you also try "$ENV{HOME}/x.pl"?
    >>

    >
    > Yes : Getting the error,
    > No such file or directory
    >


    What does 'print "Not Found: $fileToCheck\n";' tell you?

    Toni
     
    Toni Erdmann, Aug 3, 2004
    #3
  4. Abhinav

    Anno Siegel Guest

    Abhinav <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > I am reading the name of a file from STDIN.
    >
    > I am using the following :
    >
    > my $fileToCheck = <STDIN>;
    > chomp ($fileToCheck);
    > if (-e $fileToCheck)
    > {
    > print "Not Found\n";
    > }
    >
    > The problem is that I want to allow the user to specify the file with
    > environment variables in some way. For example, to check if x.pl is present
    > under the home directory, the user should be able to give
    >
    > $HOME/x.pl


    Untested:

    chomp( my $fileToCheck = <STDIN>);
    $fileToCheck =~ s/\$(\w+)/$ENV{ $1}/g;

    Is that what you're after?

    Anno
     
    Anno Siegel, Aug 3, 2004
    #4
  5. Abhinav

    Abhinav Guest

    Toni Erdmann wrote:

    > Abhinav wrote:
    >
    >>Julius Plenz wrote:
    >>
    >>
    >>>* Abhinav <> [2004-08-03]:
    >>>
    >>>
    >>>>my $fileToCheck = <STDIN>;
    >>>>chomp ($fileToCheck);
    >>>>[...]
    >>>
    >>>
    >>>
    >>>You can write that way shorter:
    >>>
    >>>chomp (my $fileToCheck = <STDIN>);
    >>>
    >>>
    >>>
    >>>>[...]
    >>>>However, it seems that we cannot interpolate like this while reading
    >>>>from STDIN (or from a file)? Am I missing something ?
    >>>>
    >>>>I tried $ENV{HOME}/x.pl also, but it also gave the same error :
    >>>
    >>>
    >>>
    >>>Did you also try "$ENV{HOME}/x.pl"?
    >>>

    >>
    >>Yes : Getting the error,
    >>No such file or directory
    >>

    >
    >
    > What does 'print "Not Found: $fileToCheck\n";' tell you?

    The file is there :

    (ab) newschema- ls $HOME/x.pl
    /home/ab/x.pl

    Test with absolute path :
    (ab) newschema- perl test.pl
    /home/ab/x.pl
    success

    Failed Tests :

    (ab) newschema- perl test.pl
    "$ENV{HOME}/x.pl"
    Not Found: "$ENV{HOME}/x.pl"

    (ab) newschema- perl test.pl
    $ENV{HOME}/x.pl
    Not Found: $ENV{HOME}/x.pl

    (ab) newschema- perl test.pl
    ~/x.pl
    Not Found: ~/x.pl

    (ab) newschema- perl test.pl
    "$HOME/x.pl"
    Not Found: "$HOME/x.pl"

    (ab) newschema- perl test.pl
    ${HOME}/x.pl
    Not Found: ${HOME}/x.pl

    (ab) newschema- perl test.pl
    "${HOME}/x.pl"
    Not Found: "${HOME}/x.pl"

    (ab) newschema- perl test.pl
    $HOME/x.pl
    Not Found: $HOME/x.pl

    There is no variable substitution happening here..
    --

    Abhinav
     
    Abhinav, Aug 3, 2004
    #5
  6. Abhinav

    Abhinav Guest

    Anno Siegel wrote:

    > Abhinav <> wrote in comp.lang.perl.misc:
    >
    >>Hi,
    >>
    >>I am reading the name of a file from STDIN.
    >>
    >>I am using the following :
    >>
    >>my $fileToCheck = <STDIN>;
    >>chomp ($fileToCheck);
    >>if (-e $fileToCheck)
    >>{
    >> print "Not Found\n";
    >>}
    >>
    >>The problem is that I want to allow the user to specify the file with
    >>environment variables in some way. For example, to check if x.pl is present
    >>under the home directory, the user should be able to give
    >>
    >>$HOME/x.pl

    >
    >
    > Untested:
    >
    > chomp( my $fileToCheck = <STDIN>);
    > $fileToCheck =~ s/\$(\w+)/$ENV{ $1}/g;
    >
    > Is that what you're after?
    >

    Exactly !

    I thought this could be done directly by reading the input, specified in
    *some* way, without having to modify it..

    This is good enough :)

    Thanks

    --
    Abhinav
     
    Abhinav, Aug 3, 2004
    #6
  7. Abhinav

    Ala Qumsieh Guest

    Abhinav wrote:
    > if (-e $fileToCheck)
    > {
    > print "Not Found\n";
    > }


    -e returns true if the file exists. You have your logic all mixed up.

    unless (-e $fileToCheck) {
    print "Not Found\n";
    }

    --Ala
     
    Ala Qumsieh, Aug 3, 2004
    #7
  8. Abhinav

    Abhinav Guest

    Ala Qumsieh wrote:
    > Abhinav wrote:
    >
    >>if (-e $fileToCheck)
    >>{
    >> print "Not Found\n";
    >>}

    >
    >
    > -e returns true if the file exists. You have your logic all mixed up.
    >


    Another reason not to type in code..even if it is just a conceptual thing.

    That should have read !e ..

    As the other posts imply, the problem lay elsewhere.

    [...]
     
    Abhinav, Aug 3, 2004
    #8
  9. On Tue, 03 Aug 2004 17:00:52 +0530, Abhinav
    <> wrote:

    >my $fileToCheck = <STDIN>;
    >chomp ($fileToCheck);
    >if (-e $fileToCheck)
    >{
    > print "Not Found\n";
    >}


    Side note: I suppose that should be !-e or s/if/unless/, etc.

    >The problem is that I want to allow the user to specify the file with
    >environment variables in some way. For example, to check if x.pl is present
    >under the home directory, the user should be able to give
    >
    >$HOME/x.pl
    >
    >However, it seems that we cannot interpolate like this while reading from
    >STDIN (or from a file)? Am I missing something ?


    Yes: environment variables expansion is done by the shell, and there's
    no reason it should take place while reading from a FH.

    >I tried $ENV{HOME}/x.pl also, but it also gave the same error :


    Because that is read literally, as is natural to expect. FWIW I would
    never do anything like that, but then you may achieve what you want by
    means of:

    s/\$\(w+)/$ENV{$1}/ge;

    e.g.:

    # export foo=bar
    # perl -lpe 's/\$(\w+)/$ENV{$1}/'
    This is $foo, isn't it?
    This is bar, isn't it?

    Oops, I forgot /e and it still works out of interpolation!


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, Aug 3, 2004
    #9
  10. On Tue, 03 Aug 2004 17:58:58 +0530, Abhinav
    <> wrote:

    >(ab) newschema- perl test.pl
    >$HOME/x.pl
    >Not Found: $HOME/x.pl
    >
    >There is no variable substitution happening here..


    And there MUST NOT be...


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, Aug 3, 2004
    #10
  11. On 3 Aug 2004 11:44:41 GMT, Julius Plenz <> wrote:

    >> I tried $ENV{HOME}/x.pl also, but it also gave the same error :

    >
    >Did you also try "$ENV{HOME}/x.pl"?


    He means that he tried writing that ON STDIN, not literally in the
    program source...


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, Aug 3, 2004
    #11
  12. Here's another thought. If you want true shell-like evaluation, you could
    literally pass your input to the shell.

    > my $fileToCheck = <STDIN>;


    $fileToCheck=`echo $fileToCheck`;

    > chomp ($fileToCheck);
    > if (-e $fileToCheck)
    > {
    > print "Not Found\n";
    > }


    This should expand "~" as well as expand environment variables and do other
    shell things. However, this is kind of quick and dirty. Not as safe as

    $fileToCheck =~ s/\$(\w+)/$ENV{ $1}/g;


    >
    > The problem is that I want to allow the user to specify the file with
    > environment variables in some way. For example, to check if x.pl is

    present
    > under the home directory, the user should be able to give
    >
    > $HOME/x.pl
    >
    > However, it seems that we cannot interpolate like this while reading from
    > STDIN (or from a file)? Am I missing something ?
    >
    > I tried $ENV{HOME}/x.pl also, but it also gave the same error :
    >
    > No such file or directory
    >
    > TIA
    >
    > --
    > Abhinav
     
    Andrew Palmer, Aug 4, 2004
    #12
    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. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    25,214
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    803
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,363
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    433
    Janos Sebok
    Jun 9, 2009
  5. phrankster

    Variable Interpolation with %%variable

    phrankster, Aug 9, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    150
    James Taylor
    Aug 14, 2005
Loading...

Share This Page