Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Variable interpolation on STDIN ?

Reply
Thread Tools

Variable interpolation on STDIN ?

 
 
Abhinav
Guest
Posts: n/a
 
      08-03-2004
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
 
Reply With Quote
 
 
 
 
Abhinav
Guest
Posts: n/a
 
      08-03-2004
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
 
Reply With Quote
 
 
 
 
Toni Erdmann
Guest
Posts: n/a
 
      08-03-2004
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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-03-2004
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
 
Reply With Quote
 
Abhinav
Guest
Posts: n/a
 
      08-03-2004
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
 
Reply With Quote
 
Abhinav
Guest
Posts: n/a
 
      08-03-2004
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
 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      08-03-2004
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

 
Reply With Quote
 
Abhinav
Guest
Posts: n/a
 
      08-03-2004
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.

[...]
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      08-03-2004
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"
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      08-03-2004
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"
 
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
peek at stdin, flush stdin Johnathan Doe C Programming 5 4 Weeks Ago 04:30 PM
How to pass stdin of a C++ program to the stdin of a process createdwith ShellExecute() Ben C Programming 2 08-29-2009 09:47 PM
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
Variable Interpolation with %%variable phrankster Perl Misc 9 08-14-2005 12:19 AM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM



Advertisments