Solved

Use of constant to open a file - not working!

Posted on 2007-12-01
4
193 Views
Last Modified: 2010-03-05
I get an error in my code when trying to use the ACTIVE_LOG2 (code failure on the open line). Why doesn't this work, or how can I use a constant for opening files? As the code shows I was able to use the READONLY module constant without any problems.
# Usage      : First need to manually create the temp.log and temp2.log files

# Purpose    :   Shows how to open files with 2 or with 3 arguments.

# Returns    : no change to temp.log and temp2.log because they are input only

#  the perm.log and perm2.log files are created and overwritten each time

# the program is run

# Parameters :

# comments: Best Practices recommends the 3 argument method for clarity,

# but other books say to use the 2 argument for backwards compatiblity
 

# See Perl Best Practices pg 55 for info on the Readonly module

# Summarized here: better alternative to using keyword constant

use Readonly;       #I had to use PPM to install this module

use Carp;           #included with Perl, need this for the croak function

#example had >temp.log but this is not a valid name for windows

Readonly my $ACTIVE_LOG1 => 'temp.log';

Readonly my $STATIC_LOG1 => 'perm.log';

use constant ACTIVE_LOG2 => 'temp2.log';

use constant STATIC_LOG2 => 'perm2.log';
 

#open modes

# < input only

# > output only

# >> open in append mode

# +< r/w update access

# +> w/r update access

# +>> read append
 

# Intention is to .... 	open temp.log for input only

# 						open perm.log for output/overwriting only

open my $active1, "<$ACTIVE_LOG1" or croak "Can't open '$ACTIVE_LOG1': $!";

open my $static1, ">$STATIC_LOG1" or croak "Can't open '$STATIC_LOG1': $!";
 
 

# Here is the 3 argument open

open my $active2, '<', "ACTIVE_LOG2" or croak "Can't open 'ACTIVE_LOG2'";

open my $static2, '>', "STATIC_LOG2" or croak "Can't open 'STATIC_LOG2'";
 
 

print $active1 "active1 line 1a";

print $static1 "static1 line 1b";

print $active2 "active2 line 1a";

print $static2 "static2 line 1b";

Open in new window

0
Comment
Question by:lqwest
  • 3
4 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 20389957
The exact working of the error message would be helpfull.

EVERY perl script you write should include these 2 pragmas.
use strict;
use warnings;

The problem is that you're trying to open a file fore reading that doesn't exist.
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 50 total points
ID: 20389975
I forgot to mention, you need to remove the quotes around the constants in the open call.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20389990
Run this updated version and fix the errors that it points out.
#!/usr/bin/perl
 

use strict;

use warnings;

use Data::Dumper;
 

# Usage      : First need to manually create the temp.log and temp2.log files

# Purpose    :   Shows how to open files with 2 or with 3 arguments.

# Returns    : no change to temp.log and temp2.log because they are input only

#  the perm.log and perm2.log files are created and overwritten each time

# the program is run

# Parameters :

# comments: Best Practices recommends the 3 argument method for clarity,

# but other books say to use the 2 argument for backwards compatiblity

 

# See Perl Best Practices pg 55 for info on the Readonly module

# Summarized here: better alternative to using keyword constant

use Readonly;       #I had to use PPM to install this module

use Carp;           #included with Perl, need this for the croak function

#example had >temp.log but this is not a valid name for windows

Readonly my $ACTIVE_LOG1 => 'temp.log';

Readonly my $STATIC_LOG1 => 'perm.log';

use constant ACTIVE_LOG2 => 'temp2.log';

use constant STATIC_LOG2 => 'perm2.log';
 

print Dumper "ACTIVE_LOG2", ACTIVE_LOG2;
 

#open modes

# < input only

# > output only

# >> open in append mode

# +< r/w update access

# +> w/r update access

# +>> read append

 

# Intention is to ....  open temp.log for input only

#                                               open perm.log for output/overwriting only

open my $active1, "<$ACTIVE_LOG1" or croak "Can't open '$ACTIVE_LOG1': $!";

open my $static1, ">$STATIC_LOG1" or croak "Can't open '$STATIC_LOG1': $!";

 

 

# Here is the 3 argument open

open my $active2, '<', "ACTIVE_LOG2" or croak "Can't open 'ACTIVE_LOG2'";

open my $static2, '>', STATIC_LOG2 or croak "Can't open 'STATIC_LOG2'";

 

 

print $active1 "active1 line 1a";

print $static1 "static1 line 1b";

print $active2 "active2 line 1a";

print $static2 "static2 line 1b";

Open in new window

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 75 total points
ID: 20395981
When you put quotes around a constant, it is looking for a file named "ACTIVE_LOG2", instead of what is in the constant.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Input from stdin for perl 6 118
perl script to count sepecial characters in a file 7 138
Perl script not writing to .csv file 6 106
iSeries PERL Scripts 7 146
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now