CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Post deleted by tc

 



tc
Novice

Jun 7, 2018, 4:35 PM

Post #1 of 10 (2838 views)
Post deleted by tc

 


Chris Charley
User

Jun 7, 2018, 6:57 PM

Post #2 of 10 (2833 views)
Re: [tc] Remove any amount of char from front and back of a string in a file [In reply to] Can't Post

I hardly work with HTML and it seems you are doing so here. So I'm sorry, but I can't recommend a good parser.

Some things I noticed in your code.

print SUB if (/$find/); should be print SUB $_ if (/$find/);

chomp($_str1 = <MY_FILE>); should be chomp(my $_str1 = <MY_FILE>)

You open MY_FILE for reading and later try to (unsuccessfully) write to it print MY_FILE $back;

The errors you are getting are 'strict' telling you that $_str1 hasn't been declared with 'my'.


(This post was edited by Chris Charley on Jun 7, 2018, 8:14 PM)


BillKSmith
Veteran

Jun 7, 2018, 9:03 PM

Post #3 of 10 (2823 views)
Re: [tc] Remove any amount of char from front and back of a string in a file [In reply to] Can't Post

The pragma 'strict' disallows three things which are otherwise allowed.

  • Undeclared Variables

  • Symbolic References

  • Bare words


  • Removing strict to eliminate error messages is ALWAYS a bad idea.


    I will not describe symbolic reference because it is almost always a bad idea. You are unlikely to try to use it by accident. Strict tells a reader of your code that you definitely are not using it.

    Without strict, undeclared variables are considered 'global'. With strict, all variables must be declared as either global (with our) or as lexical (with my).

    You will be amazed at how many spelling and typing errors this catches for us. Good practice dictates that we declare all variables with 'my', unless we have a very good reason to use 'our'. We can prevent a number of common errors by declaring all our variables in the smallest possible scope. Strict does not require this, but it does help to find missing or misplaced my's.


    Error messages about bare words are almost always telling us that we forgot the sigel ($, @, or %) before a variable name.
    Good Luck,
    Bill


    Laurent_R
    Veteran / Moderator

    Jun 7, 2018, 11:26 PM

    Post #4 of 10 (2822 views)
    Re: [tc] Remove any amount of char from front and back of a string in a file [In reply to] Can't Post

    Chris and Bill have said it all about strict, I won't add anything on that.

    I would strongly suggest that you also uncomment the use warnings; pragma. Warnings catch in a split second a lot of errors that you would otherwise have to find through long hours of debugging session. In brief, always enable strict and warnings (except possibly for one-liners).


    (This post was edited by Laurent_R on Jun 10, 2018, 11:22 PM)


    tc
    Novice

    Jun 8, 2018, 3:05 PM

    Post #5 of 10 (2811 views)
    Re: [Chris Charley] Remove any amount of char from front and back of a string in a file [In reply to] Can't Post

    I give up. I'm going to learn C. No need to waste space. The Admin may delete this thread.


    (This post was edited by tc on Jul 9, 2018, 9:36 AM)


    BillKSmith
    Veteran

    Jun 8, 2018, 8:49 PM

    Post #6 of 10 (2796 views)
    Re: [tc] Remove any amount of char from front and back of a string in a file [In reply to] Can't Post

    It is true that removing strict and warning from a correct program would not introduce any new errors. But why would you want to? They do not add any runtime penalty. They do remain on duty for every edit you make in the future. There are runtime conditions that produce warnings that are ignored if warnings are not enabled. If you really intend to ignore some warning, disable warning in a limited scope, not the whole program.

    On another topic, you should not be using function prototypes. They appear to be similar to those in "C" and other languages, but they have a very different meaning! Their purpose is not to catch errors, but rather to allow you to override certain built-in functions which would otherwise be impossible.
    Good Luck,
    Bill

    (This post was edited by BillKSmith on Jun 8, 2018, 9:07 PM)


    BillKSmith
    Veteran

    Jun 9, 2018, 7:33 AM

    Post #7 of 10 (2790 views)
    Re: [tc] Remove any amount of char from front and back of a string in a file [In reply to] Can't Post

    I finally got a chance to study your code. I am not sure what the function rm_backchar() is intended to do. I can tell you what it does do and why that is a problem.

    Please remove the prototype '($)'. Even if you make it work, it does not do what you think.

    This function opens several files.
    Reads the first line of your input file.
    Removes the newline.
    Removes the last 28 of the remaining characters.
    Stores the result in the lexical variable $back
    Closes the input file
    Prints $back to the file _2.txt.
    Closes both output files.
    Returns the result of the last statement, i.e. the status (1) of the last close.

    Back in the main program, you store that in $proc_1 and then attempt to use it as an array reference. Perl interprets that a symbolic reference which is not allowed under strict.

    Without strict, this will return an empty array. Join returns a null string. Eval is expecting perl code. I do not know what you expect.
    Good Luck,
    Bill


    tc
    Novice

    Jun 9, 2018, 5:27 PM

    Post #8 of 10 (2786 views)
    Re: [BillKSmith] Remove any amount of char from front and back of a string in a file [In reply to] Can't Post

    I give up. I'm going to learn C.


    (This post was edited by tc on Jul 9, 2018, 9:28 AM)


    BillKSmith
    Veteran

    Jun 9, 2018, 7:19 PM

    Post #9 of 10 (2773 views)
    Re: [tc] Remove any amount of char from front and back of a string in a file [In reply to] Can't Post

    Minor point first: There is no such thing as 'PERL'. The language is called 'Perl'. The interpreter that implements it is called 'perl'.

    You have not done anything to fix the problem that perl thinks is a symbolic link.

    I think you are making a big mistake in trying to learn Perl by modifying examples which you do not understand. Spend the time and money to read an introductory Perl book. I recommend "Learning Perl". It is badly out of date, but everything in it still works. (You would be missing many cool new tricks, but you can learn them later.) Do not ignore perl's own documentation. Learn to use its documentation reading tool by typing:

    Code
    perldoc perldoc


    I still have no idea at all what you expect your eval to execute. What you have is not allowed under strict. That is good, because it does not make any sense at all!
    Good Luck,
    Bill


    Laurent_R
    Veteran / Moderator

    Jun 10, 2018, 11:28 PM

    Post #10 of 10 (2721 views)
    Re: [tc] Remove any amount of char from front and back of a string in a file [In reply to] Can't Post


    In Reply To
    Quote by Laurent_R:
    Warnings catch in a split second a lot of errors that you would otherwise have to find through long hours of debugging session.
    [end quote]

    Got it Laurent! My question is; once you got your code running perfectly with ‘strict’ and ‘warning’ enabled, would it be ok to disable ‘strict’ and ‘warning’ for all future runs? Also in your reply you did mean 'ENABLE' not unable?

    Yes, I meant enable, not unable (I fixed the typo now).

    It really does no harm to keep strict and warnings enabled, so why would you want to disable them? You may actually catch things that would otherwise be harder to noticed (such as the input file having a wrong format).

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives