Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Variable interpolation on STDIN ? (http://www.velocityreviews.com/forums/t887497-variable-interpolation-on-stdin.html)

Abhinav 08-03-2004 11:30 AM

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

Abhinav 08-03-2004 12:09 PM

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

Toni Erdmann 08-03-2004 12:10 PM

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

Anno Siegel 08-03-2004 12:19 PM

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

Abhinav 08-03-2004 12:28 PM

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

Abhinav 08-03-2004 12:34 PM

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

Ala Qumsieh 08-03-2004 02:20 PM

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


Abhinav 08-03-2004 03:17 PM

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.

[...]

Michele Dondi 08-03-2004 08:48 PM

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"

Michele Dondi 08-03-2004 08:48 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57