Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Expanding Env variables.

Expand Messages
  • Damien Carbery
    ... The user is not using the shell, but providing the above examples while the perl script is running. I spoke with jab via IM and the solution was simply to
    Message 1 of 7 , Mar 6, 2006
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, "jabir ahmed" <jabirahmed@...>
      wrote:
      >
      > the problem is i am not sure which env variable he would use!
      >
      > jab
      >
      > On 3/6/06, RAJESH PETHE <rpethe@...> wrote:
      > > If the user is giving inputs to the shell then you don't need to
      > > do anything. If it's giving inputs to perl then you need to
      > > access the %ENV hash that stores all the env. Variables. For
      > > example use $ENV{HOME} to get the value of $HOME env. Variable.
      > >
      > > Regards
      > > Rajesh
      > > -----Original Message-----
      > > Hello
      > >
      > > Can anyone help me with this
      > >
      > > The user input could be
      > >
      > > /home/user/file.txt
      > > $HOME/file.txt
      > > ~/file.txt
      > >
      > > $SOMELOCATION/a.txt
      > > $APPPATH/a.conf.
      > >
      > >
      > > how could i expand the path and get the real path using perl.
      > >

      The user is not using the shell, but providing the above examples
      while the perl script is running.

      I spoke with jab via IM and the solution was simply to use 'echo' and
      backticks to get the shell to do the work:

      my $entered_string = '$SOMELOCATION/a.txt';
      my $expanded_string = `echo $entered_string`;
    • merlyn@stonehenge.com
      ... Damien The user is not using the shell, but providing the above examples Damien while the perl script is running. Damien I spoke with jab via IM and the
      Message 2 of 7 , Mar 6, 2006
      • 0 Attachment
        >>>>> "Damien" == Damien Carbery <daymobrew@...> writes:

        Damien> The user is not using the shell, but providing the above examples
        Damien> while the perl script is running.

        Damien> I spoke with jab via IM and the solution was simply to use 'echo' and
        Damien> backticks to get the shell to do the work:

        Damien> my $entered_string = '$SOMELOCATION/a.txt';
        Damien> my $expanded_string = `echo $entered_string`;

        A perfectly fine solution if you don't care about speed, security, destroying
        whitespace, or otherwise mangling shell-related metacharacters.

        In other words, the wrong solution for nearly everyone.

        The right solution is in the FAQ, which nobody seems to search before posting
        to this mailing list.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      • Victor Bouffier
        ... What Randal suggests is in perldoc -q ~ under the linux/unix command line. Windows users can search through the Activeperl documentation for the same
        Message 3 of 7 , Mar 6, 2006
        • 0 Attachment
          On Mon, 2006-03-06 at 12:19 -0800, merlyn@... wrote:
          > >>>>> "Damien" == Damien Carbery <daymobrew@...> writes:
          >
          > Damien> The user is not using the shell, but providing the above examples
          > Damien> while the perl script is running.
          >
          > Damien> I spoke with jab via IM and the solution was simply to use 'echo' and
          > Damien> backticks to get the shell to do the work:
          >
          > Damien> my $entered_string = '$SOMELOCATION/a.txt';
          > Damien> my $expanded_string = `echo $entered_string`;
          >
          > A perfectly fine solution if you don't care about speed, security, destroying
          > whitespace, or otherwise mangling shell-related metacharacters.
          >
          > In other words, the wrong solution for nearly everyone.
          >
          > The right solution is in the FAQ, which nobody seems to search before posting
          > to this mailing list.
          >

          What Randal suggests is in
          perldoc -q '~'

          under the linux/unix command line. Windows users can search through the
          Activeperl documentation for the same info.
        • Damien Carbery
          ... examples ... echo and ... destroying ... before posting ... And perlfaq4
          Message 4 of 7 , Mar 6, 2006
          • 0 Attachment
            --- In perl-beginner@yahoogroups.com, Victor Bouffier <victor@...> wrote:
            >
            > On Mon, 2006-03-06 at 12:19 -0800, merlyn@... wrote:
            > > >>>>> "Damien" == Damien Carbery <daymobrew@...> writes:
            > >
            > > Damien> The user is not using the shell, but providing the above
            examples
            > > Damien> while the perl script is running.
            > >
            > > Damien> I spoke with jab via IM and the solution was simply to use
            'echo' and
            > > Damien> backticks to get the shell to do the work:
            > >
            > > Damien> my $entered_string = '$SOMELOCATION/a.txt';
            > > Damien> my $expanded_string = `echo $entered_string`;
            > >
            > > A perfectly fine solution if you don't care about speed, security,
            destroying
            > > whitespace, or otherwise mangling shell-related metacharacters.
            > >
            > > In other words, the wrong solution for nearly everyone.
            > >
            > > The right solution is in the FAQ, which nobody seems to search
            before posting
            > > to this mailing list.
            > >
            >
            > What Randal suggests is in
            > perldoc -q '~'
            >
            > under the linux/unix command line. Windows users can search through the
            > Activeperl documentation for the same info.
            >

            And perlfaq4
            http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/lib/Pod/perlfaq4.html#how_can_i_expand_variables_in_text_strings
            How can I expand variables in text strings?

            Let's assume that you have a string that contains placeholder variables.

            $text = 'this has a $foo in it and a $bar';

            You can use a substitution with a double evaluation. The first /e
            turns $1 into $foo, and the second /e turns $foo into its value. You
            may want to wrap this in an eval: if you try to get the value of an
            undeclared variable while running under use strict, you get a fatal error.

            eval { $text =~ s/(\$\w+)/$1/eeg };
            die if $@;

            It's probably better in the general case to treat those variables as
            entries in some special hash. For example:

            %user_defs = (
            foo => 23,
            bar => 19,
            );
            $text =~ s/\$(\w+)/$user_defs{$1}/g;
          • merlyn@stonehenge.com
            ... Damien And perlfaq4 Yeah, that s actually the one I was thinking of. Damien
            Message 5 of 7 , Mar 6, 2006
            • 0 Attachment
              >>>>> "Damien" == Damien Carbery <daymobrew@...> writes:

              Damien> And perlfaq4

              Yeah, that's actually the one I was thinking of.

              Damien> http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/lib/Pod/perlfaq4.html#how_can_i_expand_variables_in_text_strings

              Huh? Why go all the way to CANADA to get a file that is ON YOUR HARD DRIVE.

              People, repeat after me: "perldoc perlfaq4". Simple, fast, safe.

              --
              Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
              <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
              Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
              See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
            Your message has been successfully submitted and would be delivered to recipients shortly.