Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I am trying to read some file(s), get its size, and if the file(s) are larger than ...

I think you want to change the order of the operations: get the file size first, then if it's bigger than $last_maxsize, open it, read $last_maxsize bytes, write that to a new version of the file:

if ( -s "$path_only/$file_name" > $last_maxsize ) { local $/ = \$last_maxsize; open IN, '<', "$path_only/$file_name"; $_ = <IN>; close IN; open OUT, '>', "$path_only/$file_name"; print OUT; close OUT; }

Looking at the code you posted, I was puzzled by this part:

while ( my $new_record = <$last_in> ) { next if -s $new_record > $last_maxsize; ...
Look up "perldoc -f -X" to read about the "-s" function -- it takes either a file handle or the name of a file. Since $new_record is neither of these things in your code, I would expect "-s" to always return 0. And since this is never greater than $last_maxsize, you will be opening an output file every time. And why use a while loop at all, if you only intend to read a single record of $last_maxize bytes?

The last issue, of course, it whether it's really wise to always truncate the file to a specific number of bytes. If it's text data, you might want to respect line boundaries (or at least word boundaries) -- and if the text is in some non-ASCII encoding (e.g. has utf8 wide characters) you should be sure to respect character boundaries; if it's compressed data, truncating it at all will make it impossible to uncompress; and so on... It's actually hard to imagine any kind of data that wouldn't suffer badly from an arbitrary fixed-length truncation like this.


In reply to Re: Delete On File Size by graff
in thread Delete On File Size by Anonymous Monk

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 contemplating the Monastery: (5)
As of 2024-04-26 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found