![]() |
Variable interpolation on STDIN ?
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 |
Re: Variable interpolation on STDIN ?
Julius Plenz wrote:
> * Abhinav <matrix_calling@yahoo.dot.com> [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 |
Re: Variable interpolation on STDIN ?
Abhinav wrote:
> Julius Plenz wrote: > >> * Abhinav <matrix_calling@yahoo.dot.com> [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 |
Re: Variable interpolation on STDIN ?
Abhinav <matrix_calling@yahoo.dot.com> 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 |
Re: Variable interpolation on STDIN ?
Toni Erdmann wrote:
> Abhinav wrote: > >>Julius Plenz wrote: >> >> >>>* Abhinav <matrix_calling@yahoo.dot.com> [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 |
Re: Variable interpolation on STDIN ?
Anno Siegel wrote:
> Abhinav <matrix_calling@yahoo.dot.com> 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 |
Re: Variable interpolation on STDIN ?
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 |
Re: Variable interpolation on STDIN ?
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. [...] |
Re: Variable interpolation on STDIN ?
On Tue, 03 Aug 2004 17:00:52 +0530, Abhinav
<matrix_calling@yahoo.dot.com> 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" |
Re: Variable interpolation on STDIN ?
On Tue, 03 Aug 2004 17:58:58 +0530, Abhinav
<matrix_calling@yahoo.dot.com> 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" |
| All times are GMT. The time now is 01:45 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.