Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The two subroutines in the code below achieve the same thing in different way. As others have pointed out, the mode +< (or O_RDWR with Fcntl module) is the one you need instead of +>> (the latter is still possible, just harder) for the first sub. The second sub uses Tie::File module that makes file operation is as simple as array operation.

The code (file locking is omitted intentionally):

#!/usr/bin/perl use strict; use warnings; my($rdwr_file, $tie_file) = @ARGV; with_rdwr(); with_tiefile(); sub with_rdwr { open DAT, "+<$rdwr_file" or die "can't open $rdwr_file: $!\n"; my $last = (<DAT>)[-1]; if ($last =~ /(\d+)/) { my $new = $1 + 1; seek DAT, 0, 2 or die "Can't seek in $rdwr_file: $!\n"; print DAT $new, "\n"; print "new value for $rdwr_file: $new\n"; } close DAT; } sub with_tiefile { use Tie::File; my @lines; tie @lines, 'Tie::File', $tie_file or die "can't tie $tie_file: $! +\n"; my $last = $lines[-1]; if ($last =~ /(\d+)/) { # sure, testing on $lines[-1] saves a line my $new = $1 + 1; push @lines, $new; print "new value for $tie_file: $new\n"; } untie @lines; }

The run:

$ echo 0 > rdwr $ echo 0 > tie $ cat rdwr 0 $ cat tie 0 $ perl prog.pl rdwr tie new value for rdwr: 1 new value for tie: 1 $ cat rdwr 0 1 $ cat tie 0 1 ... and some executions later.. $ cat rdwr 0 1 2 3 4 5 $ cat tie 0 1 2 3 4 5

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Re: Filehandles vs Uninitialized Values in pattern match by naikonta
in thread Filehandles vs Uninitialized Values in pattern match by Flubb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-29 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found