raja567 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a piece of code, which reads a table and fetches a value which is nothing but the path "$P2KTMP/data". I need to write o/p data to that particular path, but my code is failing to do so. Someone please help to fix this :(

my $sysParmSQL = "select sys_vale from system_data where sys_n +ame = 'OUT_DIR'"; my $consolidatedPath = $dbh->selectrow_array($sysParmSQL,undef +); if($consolidatedPath) { $outputPath = $consolidatedPath; if ( -d $outputPath ) ==> the path exists but it is faili +ng here and trying to go to else block { } else { print " Directory not found, Creating the directory ! +!! : $outputPath \n"; system ("mkdir $consolidatedPath"); } } else { print "WARNING : Directory System Parameter OUT_DIR not fo +und !!! Please configure the parameter \n"; exit 0; }

Replies are listed 'Best First'.
Re: Reading environment variables in perl
by Discipulus (Canon) on Feb 25, 2015 at 11:36 UTC
    Hello raja567 this qustion has noting to do with Env variables..

    Anyway i read the following in the docs of DBI:
    selectrow_array .. If called in a scalar context for a statement handle that has more tha +n one column, it is undefined whether the driver will return the valu +e of the first column or the last. So don't do that. Also, in a scala +r context, an undef is returned if there are no more rows or if an er +ror occurred. That undef can't be distinguished from an undef returne +d because the first field value was NULL. For these reasons you shoul +d exercise some caution if you use selectrow_array in a scalar contex +t, or just don't do that.
    print out your consolidatedPath to see what it holds: if the -d test fails i guess it is not a directory..
    In addition even if you'll be able to have back the value "$P2KTMP/data" where this dir is located? is a relative path? is the same relativness of your program?
    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Reading environment variables in perl
by Anonymous Monk on Feb 25, 2015 at 11:38 UTC

    The "safe and clean" way to do that would be to interpolate the environment variables from an "allowed list" into the string yourself; the example below is a little limited in that it doesn't allow escaping $FOO variables (i.e. "\$FOO" is still interpolated). The unsafe way to do it would be to have the shell do the interpolation for you, which might still be an acceptable solution if and only if you completely trust the value stored in the database; this method allows you to use the full power of the shell (dangerous!).

    $ENV{P2KTMP} = '/foo/bar'; my $consolidatedPath = '$P2KTMP/data'; my $unsafe = '$P2KTMP/data'; # Method 1 my @ALLOWED_VARS = qw/ P2KTMP FOO BAR /; my $VAR_RE = join '|', map {quotemeta} @ALLOWED_VARS; $consolidatedPath =~ s/\$($VAR_RE)\b/$ENV{$1}/g; # Method 2 chomp( $unsafe = `echo $unsafe` ); print "$consolidatedPath\n"; print "$unsafe\n"; __END__ /foo/bar/data /foo/bar/data

    By the way, you should probably use make_path from File::Path instead of system("mkdir ...")

Re: Reading environment variables in perl
by crusty_collins (Friar) on Feb 25, 2015 at 14:40 UTC
    This is untested code

    I think this is what you are having trouble with.

    use File::Path qw(make_path remove_tree); my $sysParmSQL = qq{ select sys_vale from system_data where sys_name = 'OUT_DIR' }; my $sth= $dbh->prepare($sysParmSQL); $sth->execute; my $consolidatedPath = $sth->fetchall_arrayref({}); if ( $consolidatedPath->{sys_name} ) { unless ( -d $consolidatedPath->{sys_name} ) { print "Creating the directory : $consolidatedPath\n"; make_path( $consolidatedPath); } # do something here }else{ print "WARNING : DIR not found !!! Please configure the paramete +r \n" exit(1); }
Re: Reading environment variables in perl
by soonix (Chancellor) on Feb 26, 2015 at 12:21 UTC
    Is that column really named sys_vale? From my experience I'd expect the right name to be sys_value.