assigning contents to file name............

Discussion in 'Perl' started by rxl124@hehe.com, Feb 6, 2004.

  1. Guest

    Hi,

    quick question,

    how would you assign a contents to file in perl

    i was thinking

    $filename=`echo newvalue > filename`;

    would work, but it does not, can someone please let me know what
    i am missing?

    thanks in advance

    part of the codes--------------------------------------------

    #!/usr/bin/perl -w

    $filename=`cat filename`;
    print "\$filename has $filename \n";

    if ($filename eq 'today') {
    $file_value=tomorrow;
    $filename=`echo tomorrow > filename`;}
    else {
    $file_value=today;
    $filename=`echo today > filename`;
    }
    , Feb 6, 2004
    #1
    1. Advertising

  2. wrote:
    > how would you assign a contents to file in perl


    What on earth do you mean by "assign a contents to file"?

    > i was thinking
    > $filename=`echo newvalue > filename`;
    > would work, but it does not, can someone please let me know what
    > i am missing?
    > part of the codes--------------------------------------------


    Your code doesn't even compile!

    > #!/usr/bin/perl -w
    >
    > $filename=`cat filename`;
    > print "\$filename has $filename \n";
    >
    > if ($filename eq 'today') {
    > $file_value=tomorrow;


    Unquoted string "tomorrow" may clash with future reserved word at ...

    > $filename=`echo tomorrow > filename`;}
    > else {
    > $file_value=today;


    Unquoted string "today" may clash with future reserved word at ...

    > $filename=`echo today > filename`;
    > }


    Are you simply trying to write something to a file?

    Then you may want to read
    perldoc -f open (pay particular attention the mode indicator)
    perldoc -f print
    perldoc -f close

    jue
    Jürgen Exner, Feb 6, 2004
    #2
    1. Advertising

  3. Guest

    "Jürgen Exner" <> wrote in message news:<EvJUb.13996$>...
    > wrote:
    > > how would you assign a contents to file in perl

    >
    > What on earth do you mean by "assign a contents to file"?
    >
    > > i was thinking
    > > $filename=`echo newvalue > filename`;
    > > would work, but it does not, can someone please let me know what
    > > i am missing?
    > > part of the codes--------------------------------------------

    >
    > Your code doesn't even compile!
    >
    > > #!/usr/bin/perl -w
    > >
    > > $filename=`cat filename`;
    > > print "\$filename has $filename \n";
    > >
    > > if ($filename eq 'today') {
    > > $file_value=tomorrow;

    >
    > Unquoted string "tomorrow" may clash with future reserved word at ...
    >
    > > $filename=`echo tomorrow > filename`;}
    > > else {
    > > $file_value=today;

    >
    > Unquoted string "today" may clash with future reserved word at ...
    >
    > > $filename=`echo today > filename`;
    > > }

    >
    > Are you simply trying to write something to a file?
    >
    > Then you may want to read
    > perldoc -f open (pay particular attention the mode indicator)
    > perldoc -f print
    > perldoc -f close
    >
    > jue


    I am trying to do (task A) on one day and (Task B) on next
    day(alternating).
    I thought about how to do it, and thought that only way to do it would
    be
    to write what whether I did A or B and put that value into file(let's
    say file_name, and put A since I did A today) and next day I would do
    what's opposite based on what's in that file(file_name, since A is in
    this file, I would now do B ). and run this for 24x7.....

    I didn't want to run the open/print/close commands. Was just wondering
    if there was simple way to do it or just use ` ` to excute unix
    command and assign to the file name.
    , Feb 7, 2004
    #3
  4. On 6/2/04 9:09 am, in article
    , ""
    <> wrote:

    > Hi,
    >
    > quick question,
    >
    > how would you assign a contents to file in perl
    >
    > i was thinking
    >
    > $filename=`echo newvalue > filename`;
    >
    > would work, but it does not, can someone please let me know what
    > i am missing?
    >
    > thanks in advance
    >
    > part of the codes--------------------------------------------
    >
    > #!/usr/bin/perl -w
    >
    > $filename=`cat filename`;
    > print "\$filename has $filename \n";
    >
    > if ($filename eq 'today') {
    > $file_value=tomorrow;
    > $filename=`echo tomorrow > filename`;}
    > else {
    > $file_value=today;
    > $filename=`echo today > filename`;
    > }


    evaluating the backticks operator in a scalar context
    returns the success result of the command executed

    you could use..

    use strict;
    my @ex_filename = `cat filename`' # 'filename' is fully qualified
    my $contents = $ex_filename[0]; # we're assuming lots about 'cat' here!
    ($contents =~ /today/) ? `echo tomorrow > filename` : `echo today >
    filename`;
    exit;


    not much checking going on though! would like to see some error
    catching if the file isn't there etc. :)

    david
    emology.com
    david scholefield, Feb 29, 2004
    #4
  5. Joe Smith Guest

    david scholefield wrote:

    >>$filename=`cat filename`;
    >>print "\$filename has $filename \n";

    >
    > evaluating the backticks operator in a scalar context
    > returns the success result of the command executed


    No, it does not. It returns the entire contents of the output
    from the command; all the lines concatinated into a single string.
    The success result is in $?, as described in
    perldoc perlvar


    -Joe
    Joe Smith, Mar 1, 2004
    #5
  6. On 1/3/04 9:31 am, in article 4qD0c.8525$ko6.196434@attbi_s02, "Joe Smith"
    <> wrote:

    > david scholefield wrote:
    >
    >>> $filename=`cat filename`;
    >>> print "\$filename has $filename \n";

    >>
    >> evaluating the backticks operator in a scalar context
    >> returns the success result of the command executed

    >
    > No, it does not. It returns the entire contents of the output
    > from the command; all the lines concatinated into a single string.
    > The success result is in $?, as described in
    > perldoc perlvar
    >
    >
    > -Joe


    Yup - you're quite right. *duh* Never do it personally, always
    read into list :)
    david scholefield, Mar 1, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Cox [Microsoft MVP]

    Re: Assigning contents within HTML tag to string variable.

    Ken Cox [Microsoft MVP], Aug 5, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    887
    tonymjohn
    Aug 7, 2004
  2. Don Adams
    Replies:
    1
    Views:
    581
    Martin Honnen
    Mar 5, 2004
  3. Kamarulnizam Rahim
    Replies:
    4
    Views:
    204
    Robert Klemme
    Jan 28, 2011
  4. Alexander Heimann

    parsing file name assigning extension to a variable

    Alexander Heimann, Jun 11, 2004, in forum: Perl Misc
    Replies:
    18
    Views:
    252
    Joe Smith
    Jun 18, 2004
  5. weston
    Replies:
    1
    Views:
    239
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page