Variable interpolation on STDIN ?

A

Abhinav

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
 
A

Abhinav

Julius said:
* Abhinav said:
my $fileToCheck = <STDIN>;
chomp ($fileToCheck);
[...]


You can write that way shorter:

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

Toni Erdmann

Abhinav said:
Julius said:
* Abhinav said:
my $fileToCheck = <STDIN>;
chomp ($fileToCheck);
[...]



You can write that way shorter:

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

Anno Siegel

Abhinav said:
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
 
A

Abhinav

Toni said:
Abhinav said:
Julius Plenz wrote:

* Abhinav <[email protected]> [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..
 
A

Abhinav

Anno said:
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
 
A

Ala Qumsieh

Abhinav said:
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
 
A

Abhinav

Ala said:
-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.

[...]
 
M

Michele Dondi

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
 
A

Andrew Palmer

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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top